<?php

/**
 * Framework_Presenter_Smarty
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/

require_once 'Smarty/Smarty.class.php';

/**
 * Framework_Presenter_Smarty
 *
 * By default we use Smarty as our websites presentation layer (view). Smarty
 * is a robust compiling template engine with an active community.
 *
 * @author Application Support Group
 * @package BAMF
 * @link http://smarty.php.net
*/
class Framework_Presenter_Smarty extends Framework_Presenter_Common {

	protected $template = null;
	protected $requestedTemplate = null;
	private $pageTemplateFile = null ;
	private $config = null ;
	protected $reservedVariableNames = array() ;

	public function __construct(Framework_Module $module) {
		parent::__construct($module);
		
		$this->path = SMARTY_TEMPLATE_PATH.SMARTY_TEMPLATE;
		$this->template = new Smarty();
		$this->template->template_dir = $this->path;
		$this->template->compile_dir = SMARTY_COMPILE_DIR;
		$this->template->cache_dir = SMARTY_CACHE_DIR;
		$this->template->config_dir = SMARTY_CONFIG_DIR;
		$this->template->debugging = SMARTY_DEBUGGING ;

		// register plugins
		
		Framework_Presenter_Smarty_AssignAdv::register( $this->template ) ;
		Framework_Presenter_Smarty_CharDecode::register( $this->template ) ;
		Framework_Presenter_Smarty_CharEncode::register( $this->template ) ;
		Framework_Presenter_Smarty_DebugOutput::register( $this->template ) ;
		Framework_Presenter_Smarty_IncludePhp::register( $this->template ) ;
		Framework_Presenter_Smarty_PagingInterface::register( $this->template ) ;
		Framework_Presenter_Smarty_SearchBar::register( $this->template ) ;
		Framework_Presenter_Smarty_ShowDebug::register( $this->template ) ;
		Framework_Presenter_Smarty_SimpleSearchBar::register( $this->template ) ;
		Framework_Presenter_Smarty_TableBuilder::register( $this->template ) ;

		if( file_exists( CONFIG_INI )) {
			$this->config = parse_ini_file( CONFIG_INI, true ) ;
		}

		$this->pageTemplateFile = SMARTY_DEFAULT_TEMPLATE_FILE ;
		if ( !empty( $module->pageTemplateFile )) {
			$this->pageTemplateFile = $module->pageTemplateFile ;
		} else {
			if ( isset( $this->config['default_smarty_tpl'][$this->module->moduleName] ) && !empty( $this->config['default_smarty_tpl'][$this->module->moduleName] )) {
				$this->pageTemplateFile = $this->config['default_smarty_tpl'][$this->module->moduleName] ;
			}
		}
				
		$this->reservedVariableNames = array(
			'modulePath',
			'moduleClass', 
			'requestedTemplate', 
			'originalRequest', 
			'requestedTemplate', 
			'moduleUuid',
			'itemNumber'
		) ;
	}



	public function display() {

		$modulePath = get_class( $this->module ) ;		
		$modulePath = str_replace( '_', '/', $modulePath ); 
		$requestedTemplate = SMARTY_TEMPLATE_PATH.$modulePath.'.tpl' ;
		$i18nTemplate = SMARTY_TEMPLATE_PATH . $modulePath . '.' . $this->module->i18n->langPreference . '.tpl' ;
		$modulePath = str_replace( 'Module/', FRAMEWORK_BASE_MODULE_PATH, $modulePath ); 

		$this->template->assign( 'modulePath', $modulePath ) ;
		$this->template->assign( 'moduleClass', get_class( $this->module )) ;
		$this->template->assign( 'moduleUuid', $this->module->uuid ) ;
		$this->template->assign( 'i18n', $this->module->i18n ) ;


		// Check for a special internationalized version of the template file.
		// If that doesn't exist then we'll fall back on the default requested template.
		

		if ( file_exists( $i18nTemplate )) {
			$this->template->assign('requestedTemplate', $i18nTemplate) ;
		} elseif (file_exists($requestedTemplate)) {
			$this->template->assign('requestedTemplate', $requestedTemplate) ;
		} else {
			$this->template->assign('originalRequest', $requestedTemplate) ;
			$this->template->assign('requestedTemplate', '' ) ;
		}

		foreach ($this->module->getData() as $var => $val) {
 			if (!in_array( $var, $this->reservedVariableNames ) ) {
				$this->template->assign($var,$val);
 			}
		}

		if ( isset( $this->module->itemNumber )) {
			$this->template->assign( 'itemNumber', $this->module->itemNumber ) ;
		}

		$this->template->display($this->pageTemplateFile);

		return true ;
	}

	public function __destruct()
	{
		parent::__destruct();
	}
}


?>
